package me.zhengjie.dto;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import lombok.Data;
import me.zhengjie.entity.HttpConfig;
import me.zhengjie.mapper.HttpConfigAgvManagerMapper;
import org.springframework.stereotype.Component;

import java.util.Optional;

//@ConfigurationProperties(prefix = "ipdto")
@Component
@Data
public class IpDto {

    private HttpConfigAgvManagerMapper mapper;

    private String ip = "http://127.0.0.1";

    private String part = "8888";


    public IpDto(HttpConfigAgvManagerMapper mapper) {
        this.mapper = mapper;
        Optional<HttpConfig> tuskIpPort = new LambdaQueryChainWrapper<>(mapper)
                .eq(HttpConfig::getConfigName, "TUSK_IP_PORT")
                .oneOpt();

        if (tuskIpPort.isPresent()) {
            HttpConfig httpConfig = tuskIpPort.get();
            this.ip = httpConfig.getConfigIp();

        }
    }

    public String getIpUrl() {
        Optional<HttpConfig> tuskIpPort = new LambdaQueryChainWrapper<>(mapper)
                .eq(HttpConfig::getConfigName, "TUSK_IP_PORT")
                .oneOpt();

        if (tuskIpPort.isPresent()) {
            HttpConfig httpConfig = tuskIpPort.get();
            this.ip = httpConfig.getConfigIp();
            return httpConfig.getConfigIp();
        } else {
            return null;
        }
    }
}
